package com.leetcode.problems014;


/**
 * @Date 2021/11/20 14:34
 * @Author ghery
 * @Version V1.0
 */
public class Solution {

    public String longestCommonPrefix(String[] strs) {
        if(strs == null || strs.length == 0){
            return "";
        }
        if(strs.length == 1){
            return strs[0];
        }

        String baseStrr = strs[0];
        int baseLen = baseStrr.length();
        int i = 0;
        b: for( ;i < baseLen;i++){
            for (int j = 1; j < strs.length; j++) {
                if(i >= strs[j].length()){
                    break b;
                }
                if(strs[j].charAt(i) != baseStrr.charAt(i)){
                    break b;
                }
            }
        }
        return baseStrr.substring(0,i);
    }
    public static void main(String[] args) {
        String[] strs = {"dog","racecar","car"};
        String[] strs1 = {"flower","flow","flight"};
        System.out.println(new Solution().longestCommonPrefix(strs));
        System.out.println(new Solution().longestCommonPrefix(strs1));

    }
}
